
global datapath "~/DataFolder"
global Analyses "~/Results"


************************************** Table 4: Industry Sales to County Economy
{
cd "$datapath"
*** 1. Income
use CountyJob_All, clear

keep if selfpct>= 0.05
keep if year>=1999 & year<=2017

merge m:1 naics3 year using naics3_sale
keep if _merge==3   
drop _merge  

*emp weighted average
gen c_log_sale_naics3_weight=c_log_sale_naics3_v1*selfpct
bys fips year : egen c_log_sale_county_wmean = sum(c_log_sale_naics3_weight)  
keep fips year c_log_sale_county_wmean
duplicates drop fips year, force

winsor2 c_log_sale_county_wmean, cut(2.5 97.5) by(year) replace

egen m = mean(c_log_sale_county_wmean)
egen s = sd(c_log_sale_county_wmean)
gen std_salegrow = (c_log_sale_county_wmean - m) / s

sort fips year
tsset fips year

merge 1:1 fips year using CountyCharacteristics.dta
keep if _merge==3
drop _merge

replace incomegrowth = incomegrowth*100
gen log_population=log(1+population)
replace bachelor = bachelor/100

winsor2 incomegrowth log_population above65 male minority bachelor  , cut(2.5 97.5) by(year) replace

cd "$Analyses"

reg incomegrowth std_salegrow , cluster(fips)
outreg2 using Table4.xls, replace label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) ctitle("Income Growth (%)")  addtext(County FE, No, Year FE, No) 
reg incomegrowth std_salegrow log_population above65 male minority bachelor , cluster(fips)
outreg2 using Table4.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) ctitle("Income Growth (%)")  addtext(County FE, No, Year FE, No) 
reghdfe incomegrowth std_salegrow log_population above65 male minority bachelor , absorb(fips year) cluster(fips)
outreg2 using Table4.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) ctitle("Income Growth (%)")  addtext(County FE, Yes, Year FE, Yes) 




*** 2. Deliquency (2008 to 2017 sample)
use CountyJob_All, clear

keep if selfpct>= 0.05

tabulate year  
keep if year>=2008 & year<=2017

merge m:1 naics3 year using naics3_sale
keep if _merge==3   
drop _merge  

*emp weighted average
gen c_log_sale_naics3_weight=c_log_sale_naics3*selfpct
bys fips year : egen c_log_sale_county_wmean = sum(c_log_sale_naics3_weight)  
keep fips year c_log_sale_county_wmean
duplicates drop fips year, force

winsor2 c_log_sale_county_wmean, cut(2.5 97.5) by(year) replace

egen m = mean(c_log_sale_county_wmean)
egen s = sd(c_log_sale_county_wmean)
gen std_salegrow = (c_log_sale_county_wmean - m) / s

sort fips year
tsset fips year

merge 1:1 fips year using CountyCharacteristics
keep if _merge==3
drop _merge

gen log_population=log(1+population)
replace bachelor = bachelor/100

winsor2 log_population above65 male minority bachelor  , cut(2.5 97.5) by(year) replace

merge 1:1 fips year using Delinquency.dta
keep if _merge==3
drop _merge

gen c_delin_rate=(delin_rate_30to89+delin_rate_90plus)-(lag_delin_rate_30to89+lag_delin_rate_90plus)
winsor2 c_delin_rate , cut(2.5 97.5) by(year) replace

cd "$Analyses"

reg c_delin_rate std_salegrow,cluster(fips)
outreg2 using Table4.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) ctitle("Delta Mortgatge Deliquency") addtext(County FE, No, Year FE, No) 
reg c_delin_rate std_salegrow log_population above65 male minority bachelor ,  cluster(fips)
outreg2 using Table4.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) ctitle("Delta Mortgatge Deliquency") addtext(County FE, No, Year FE, No) 
reghdfe c_delin_rate std_salegrow log_population above65 male minority bachelor , absorb(fips year) cluster(fips)
outreg2 using Table4.xls, append label dec(3) drop(num) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) ctitle("Delta Mortgatge Deliquency") addtext(County FE, Yes, Year FE, Yes) 

}

